package com.fa.wisdom.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.base.BaseFragment;
import com.fa.wisdom.common.App;
import com.fa.wisdom.listen.ConfirmListen;
import com.fa.wisdom.model.ResultEntity;
import com.fa.wisdom.net.MyCallback;
import com.fa.wisdom.net.Task;
import com.fa.wisdom.ui.HelperActivity;
import com.fa.wisdom.ui.LoginActivity;
import com.fa.wisdom.ui.StaticActivity;
import com.fa.wisdom.ui.UserInfoActivity;
import com.fa.wisdom.utils.CenterDialog;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.ScreenUtil;
import com.fa.wisdom.utils.SpUtils;
import com.fa.wisdom.utils.ToastUtils;
import com.tuya.appsdk.sample.resource.HomeModel;
import com.tuya.smart.android.user.api.ILogoutCallback;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Response;

public class MineFragment extends BaseFragment implements View.OnClickListener{
    View statusBarPlace;
    ImageView iv_set;
    CircleImageView ivhead;
    TextView tv_1,tv_2,tv_3,tv_4,tv_log_out,tvName;
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        statusBarPlace = view.findViewById(R.id.view_status_bar_place);
        tv_1=view.findViewById(R.id.tv_1);
        tv_2=view.findViewById(R.id.tv_2);
        tv_3=view.findViewById(R.id.tv_3);
        ivhead=view.findViewById(R.id.civ_head);
        tv_4=view.findViewById(R.id.tv_4);
        tvName=view.findViewById(R.id.tvName);
        tv_log_out=view.findViewById(R.id.tv_log_out);
        iv_set=view.findViewById(R.id.iv_set);
        ViewGroup.LayoutParams params = statusBarPlace.getLayoutParams();
        params.height = ScreenUtil.getStatusBarHeight(getContext());
        statusBarPlace.setLayoutParams(params);
        tv_1.setOnClickListener(this);
        tv_2.setOnClickListener(this);
        tv_3.setOnClickListener(this);
        tv_4.setOnClickListener(this);
        iv_set.setOnClickListener(this);
        tv_log_out.setOnClickListener(this);
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getUserInfo();
        }
    }
    private void getUserInfo(){
        LoadingDialog loadingDialog= Loading.getInstance().onShowLargeLoading("努力加载中...");
        loadingDialog.showInActivity(requireActivity());
        Task.getApiService().getUserInfo().enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response<ResultEntity> response) {
                loadingDialog.dismiss();
                String jsonString = JSON.toJSONString(response.body());
                JSONObject jsonObject = JSON.parseObject(jsonString);
                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                String name=jsonObject1.getString("name");
                String head=jsonObject1.getString("avatarurl");
                tvName.setText(name);
                Glide.with(App.getContext()).load(TextUtils.isEmpty(head)?R.drawable.mine_head:head)
                        .placeholder(R.drawable.mine_head)
                        .error(R.drawable.mine_head)
                        .into(ivhead);
            }

            @Override
            protected void onFailure(Throwable t) {
                loadingDialog.dismiss();
                ToastUtils.showError("获取用户信息失败");
            }
        });
    }
    @Override
    public View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_mine, null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_1:
                startActivity(new Intent(requireContext(), UserInfoActivity.class));
                break;
            case R.id.tv_2:
                startActivity(new Intent(requireContext(), HelperActivity.class).putExtra("detail","帮助中心"));
                break;
            case R.id.tv_3:
                startActivity(new Intent(requireContext(), HelperActivity.class).putExtra("detail","咨询客服"));
                break;
            case R.id.tv_4:
                startActivity(new Intent(requireContext(), HelperActivity.class).putExtra("detail","关于我们"));
                break;
            case R.id.iv_set:
                startActivity(new Intent(requireContext(), StaticActivity.class));
                break;
            case R.id.tv_log_out:
                new CenterDialog(R.layout.prompt_dialog, requireActivity()).showPrompt("确认退出？", new ConfirmListen() {
                    @Override
                    public void onConfirm() {
                        //确定之后操作
                        logOut();
                    }
                });
                break;
        }
    }
    //退出操作
    LoadingDialog loadingDialog;
    private void logOut(){
        loadingDialog = Loading.getInstance().onShowLargeLoading("正在安全退出...");
        loadingDialog.showInActivity(requireActivity());
        TuyaHomeSdk.getUserInstance().logout(new ILogoutCallback() {
            @Override
            public void onSuccess() {
                // Clear cache
                loadingDialog.dismiss();
                HomeModel.INSTANCE.clear(App.getContext());
                SpUtils.getInstance().removeAllKey();
                // Navigate to User Func Navigation Page
                Intent intent = new Intent(requireActivity(), LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                requireActivity().finish();
            }

            @Override
            public void onError(String errorCode, String errorMsg) {
                loadingDialog.dismiss();
                ToastUtils.showError("安全退出失败，请稍后再试");
            }
        });
    }
}
